package com.httpserver.one;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 *@project:HttpServerDemo
 *@author   wuhn
 *@version  1.0
 *@createTime:2017年10月18日 下午12:18:12
 *@description: Web 服务器线程启动 线程
 */
public class HttpThread extends Thread {
	private Socket  socket;

	public HttpThread(Socket socket) {
		super();
		this.socket = socket;
	}
	

	@Override
	public void run(){
		InputStream  ins =null;
		OutputStream outs=null;
		try {
			ins= socket.getInputStream();
			outs=socket.getOutputStream();
			
			Receive  rcv =new Receive(ins);
			final String  sURL = rcv.parse(); //用 receive 类取得浏览器发过来的URL请求。
			if(sURL.startsWith("/")){
				Answer ans =new Answer(outs);
				ans.send(sURL); 
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(ins != null){
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(outs !=null){
				try {
					outs.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(socket != null){
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	
	
}
